package com.skyworth.skymanualActivity;
import java.util.Locale;
import com.skyworth.skymanualdata.SkyworthKeyCode;
import com.skyworth.skymanualview.BaseItemView;
import com.skyworth.skymanualview.MetroLayout;
import com.skyworth.skymanual.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
public class GuideListActivity extends Activity {
	ImageButton imageremote,imagemovie,imagegame,imageshopping,
			  imagescreen,imagesetting,imageinstall,imagequestion;
	 private static Integer  colors[] = {
		                                 R.drawable.corner_top_one,
		                                 R.drawable.corner_top_two,
		                                 R.drawable.corner_top_three,
		                                 R.drawable.corner_top_four,
		                                 R.drawable.corner_bottom_one,
		                                 R.drawable.corner_bottom_two,
		                                 R.drawable.corner_bottom_three,
		                                 R.drawable.corner_bottom_four
		                                 };
	private BaseItemView  mbaseViewItems [] = new BaseItemView[8];
    private MetroLayout metroLayout;
	private Context mContext;
	private RelativeLayout contentView;
    public static float DIV_W;
    public static float DIV_H;
    public static  float density;
	String strname;
	Intent intent;	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext=this;
		initDisplayDiv();	
		contentView=new RelativeLayout(mContext);
		contentView.setBackgroundResource(R.drawable.newhome);
		requestWindowFeature(Window.FEATURE_NO_TITLE);	
		RelativeLayout.LayoutParams content_p=new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.FILL_PARENT);
		this.addContentView(contentView, content_p);   
		
		
		metroLayout = new MetroLayout(mContext);
		metroLayout.setId(707);
		RelativeLayout.LayoutParams l_p = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
		
        l_p.topMargin=(int)(260*GuideListActivity.DIV_H/density);
        l_p.leftMargin=(int)(90*GuideListActivity.DIV_W/density);
        contentView.addView(metroLayout, l_p); 
        
        
        
	    TextView titleText=new TextView(mContext);
	    titleText.setTextSize(70*GuideListActivity.DIV_W/density);
	    titleText.setText("玩机手册");
	    titleText.setTextColor(Color.WHITE);
	    RelativeLayout.LayoutParams title_p=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
	    		 LayoutParams.WRAP_CONTENT);
	    title_p.topMargin=(int)(160*GuideListActivity.DIV_H/density);
	    title_p.leftMargin=(int)(162*GuideListActivity.DIV_W/density);
	    contentView.addView(titleText, title_p);  
	    
	    TextView markText=new TextView(mContext);
	    markText.setTextSize(30*GuideListActivity.DIV_W/density);
	    markText.setText("注：因产品版本持续更新，玩机手册内容或与实际有所差别，请以实际产品为准");
	    markText.setTextColor(Color.WHITE);
	    RelativeLayout.LayoutParams mark_p=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
	    		 LayoutParams.WRAP_CONTENT);
	    mark_p.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
	    mark_p.bottomMargin=(int)(60*GuideListActivity.DIV_H/density);
	    mark_p.leftMargin=(int)(162*GuideListActivity.DIV_W/density);
	    contentView.addView(markText, mark_p);  
	    
	    
		metroLayout.removeAllViews();
        metroLayout.cleanContainer();
        metroLayout.setSize(2);
        initView();		
        
        for(int i=0;i<8;i++){
        	mbaseViewItems[i].setOnClickListener(new MyImageViewListener());
        	mbaseViewItems[i].setBackColor(colors[i]);
        	if(i<=3){
        	metroLayout.addSubView(mbaseViewItems[i], 0,i);
        	}else{
        		metroLayout.addSubView(mbaseViewItems[i], 1, i-4);
        	}        	
        }
      intent = new Intent();
      if(isEn()){
    	  Toast.makeText(this, "The English is not supported.", Toast.LENGTH_LONG).show();
    	  Log.i("lhy","~~~~~~~~~~~~~~~~~~~~~~~~~~~~English!");
      }else{
    	  Log.i("lhy","~~~~~~~~~~~~~~~~~~~~~~~~~~~~Chinese!");
      }

	}
    private void initView(){   	
		for(int i=0;i<8;i++){
			if(mbaseViewItems[i]==null){
			     mbaseViewItems[i] = new BaseItemView(mContext, (int) (420*DIV_W), (int) (365*DIV_H),
					i+1,1);
			}
		}
    	
    }
    
	public void initDisplayDiv()
    {
        Display display = ((WindowManager) mContext
                .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        float screenWidth = display.getWidth();   
        float screenHeight = display.getHeight();  
        DIV_W = screenWidth / 1920f;
        DIV_H = screenHeight / 1080f;                 
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width = metric.widthPixels; 
        int height = metric.heightPixels;
        density = metric.density;  // 0.75 / 1.0 / 1.5
        int densityDpi = metric.densityDpi; //120/160/240     
        
        Log.v("lhy","screenWidth="+screenWidth+"  ; screenHeight="+screenHeight);
        Log.v("lhy","****DIV_W="+DIV_W+"   ;"+"DIV_H="+DIV_H);
        Log.v("lhy","****width="+width+"  ;"+"height="+height);
        Log.v("lhy","****density="+density+"  ;"+"densityDpi="+densityDpi);
        
    }
	public class MyImageViewListener implements OnClickListener{
		@Override
		public void onClick(View v) {
			switch(v.getId())
			{
				case 0:					
					strname = "common";
					intent.putExtra("type",strname);					
                    break;
				case 1:
					strname = "setting";
					intent.putExtra("type",strname);	
					break;
				case 2:							
					strname = "AirSharing";
					intent.putExtra("type",strname);					
					break;
				case 3:		
					strname = "play";
					intent.putExtra("type",strname);					
					break;
				case 10:
					strname = "admin";
					intent.putExtra("type",strname);			
					break;
				case 11:	
					strname = "movie";
					intent.putExtra("type",strname);
					break;
				case 12:
					strname = "media";
					intent.putExtra("type",strname);				
					break;
				case 13:						
					strname = "question";
					intent.putExtra("type",strname);								
					break;
				default:
					break;
			}		
			intent.setClass(GuideListActivity.this, SecondActivity.class);			
			GuideListActivity.this.startActivity(intent);		
		}
		
	}
	
	private boolean isEn() {
        Locale locale = getResources().getConfiguration().locale;
        String language = locale.getLanguage();
        if (language.endsWith("en"))
            return true;
        else
            return false;
    }
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Log.i("lhy","KeyCode="+keyCode);
		// TODO Auto-generated method stub
		switch (keyCode) 
		{
			case SkyworthKeyCode.SKY_KEY_BACK: 
			{
				/************按退出按键直接结束整个Activity进程**************/
/*				Dialog dialog = new AlertDialog.Builder(GuideListActivity.this)
				.setTitle("提示")
				.setMessage("确定要退出吗？")
				.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,int which) 
							{
								dialog.dismiss();*/
								//GuideListActivity.this.finish();
								System.exit(0);
/*							}
						})
				.setNegativeButton("取消",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,int which) 
							{
								dialog.dismiss();
							}
						}).create();
				dialog.show();*/
			}
			return true;
		case SkyworthKeyCode.SKY_KEY_SHORT_SHUTTLE_RIGHT:
			Log.i("lhy","feisuo~~~~~~~~~~~~~~~~~~~~~SHORT_SHUTTLE_RIGHT");
			Log.i("lhy","metroLayout.getFocusId="+metroLayout.getFocusedChild().getId());
            if (!metroLayout.hasFocus())
            {
                return true;
            }
            if (metroLayout.hasFocus())
            {
            	int id = metroLayout.getFocusedChild().getId();
            	if(id==3){
            		id=9;
            	}
            	if(id<=3||id<=13){
                    
                    metroLayout.setFocusItem(id + 1);
                    return false;
                }
            }
            break;
		case SkyworthKeyCode.SKY_KEY_SHORT_SHUTTLE_LEFT :
			{
				Log.i("lhy","feisuo~~~~~~~~~~~~~~~~~~~~~SHORT_SHUTTLE_LEFT");
				Log.i("lhy","metroLayout.getFocusId="+metroLayout.getFocusedChild().getId());
                if (!metroLayout.hasFocus())
                {
                    return true;
                }
                if (metroLayout.hasFocus())
                {   
                	int id =metroLayout.getFocusedChild().getId();
                    if(id==10){
                		id=4;
                	}
                	if(id>=10||id>=0)
                	{
                		
                        metroLayout.setFocusItem(id - 1);
                        return false;
    				}               
                }
                
			}
	    break;

			default:
				break;
		}

		return super.onKeyDown(keyCode, event);
	}


}

